﻿using Kaskelot.SP.Model.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Kaskelot.SP.Services.Client
{
    [ServiceContract(Namespace = "http://Kaskelot.SP.Services")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(MaxItemsInObjectGraph = 2147483647)]
    public class ClientFormService : BaseClientService
    {
        [OperationContract]
        [WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public ClientServiceResult<string> GetForm(string formName, string idString, string parentIdString)
        {
            return GetFormMarkup("/_CONTROLTEMPLATES/Kaskelot.SP.UI/Forms/" + formName + ".ascx", idString, parentIdString);
        }

		[OperationContract]
		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		public ClientServiceResult<string> GetFormByPost(string formName, string[] idList)
		{
			return GetFormMarkupPost("/_CONTROLTEMPLATES/Kaskelot.SP.UI/Forms/" + formName + ".ascx", idList);
		}

		//[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
		//public ClientServiceResult<string> GetDisplayForm(string formName, string idString)
		//{
		//	return GetFormMarkup("/_CONTROLTEMPLATES/Kaskelot.SP.UI/DisplayForms/" + formName + ".ascx", idString);
		//}

		//[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
		//public ClientServiceResult<string> GetListing(string formName, string idString)
		//{
		//	return GetFormMarkup("/_CONTROLTEMPLATES/Kaskelot.SP.UI/Listings/" + formName + ".ascx", idString);
		//}

        private ClientServiceResult<string> GetFormMarkup(string userControlPath, string itemId, string parentId)
        {
            ClientServiceResult<string> csr = new ClientServiceResult<string>();

            try
            {
                Page pageHolder = new Page();
                UserControl viewControl = (UserControl)pageHolder.LoadControl(userControlPath);

                if (viewControl is Kaskelot.SP.Model.Contracts.IForm)
                {
					if(!string.IsNullOrEmpty(itemId))
						((Kaskelot.SP.Model.Contracts.IForm)viewControl).Id = itemId;
					if (!string.IsNullOrEmpty(parentId))
						((Kaskelot.SP.Model.Contracts.IForm)viewControl).ParentId = parentId;
                }

                pageHolder.Controls.Add(viewControl);

                StringWriter output = new StringWriter();
                HttpContext.Current.Server.Execute(pageHolder, output, false);

                csr.Data = output.ToString();
            }
            catch (Exception ex)
            {
                HandleGeneralError<string>(csr, ex, "GetFormMarkup", "Formuläret kunde inte laddas");
            }

            return csr;
        }

		private ClientServiceResult<string> GetFormMarkupPost(string userControlPath, string[] idList)
		{
			ClientServiceResult<string> csr = new ClientServiceResult<string>();

			try
			{
				Page pageHolder = new Page();
				UserControl viewControl = (UserControl)pageHolder.LoadControl(userControlPath);

				if (viewControl is Kaskelot.SP.Model.Contracts.IPostForm)
				{
					if (idList != null)
					{
						((Kaskelot.SP.Model.Contracts.IPostForm)viewControl).IdList = idList;
					}
				}

				pageHolder.Controls.Add(viewControl);

				StringWriter output = new StringWriter();
				HttpContext.Current.Server.Execute(pageHolder, output, false);

				csr.Data = output.ToString();
			}
			catch (Exception ex)
			{
				HandleGeneralError<string>(csr, ex, "GetFormMarkupPost", "Formuläret kunde inte laddas");
			}

			return csr;
		}
    }
}
